const fetch = require('../../utils/util.js');
const query = fetch.fetch;
const api = fetch.api;
const formatTime = fetch.formatTime 


Component({
  data: {
    showModal: false,
    dataMsg:[],
    tempFilePaths: '',//上传图片
  },

  ready: function () {
    wx.getStorage({
      key: 'openid',
      success: (res) => {
        const data = query(api.gettobedone, 'post', { openId: res.data, orderStatus: 2 });
        
        data.then(data => {
          
          for (let a of data) {
            a.startDate = formatTime(a.startDate)
          }
          this.setData({
            dataMsg: data
          })

        })

      }
    })
  },
  // 弹窗
  methods:{
    showDialogBtn: function () {
       this.setData({
      showModal: true
    })
  },
   //百公里油耗
    bindstar(e) {
      this.setData({
        endOil: e.detail.value
      })
    },
    //结束行程 
    bindend(e) {
      this.setData({
        endKm: e.detail.value
      })
    },
    //点击上传图片
    chooseimage(){
      var _that = this
      wx.chooseImage({       
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths[0]
          wx.uploadFile({
            //上传地址
            url: "http://192.168.1.66:8080/driverupload/app",
            filePath: tempFilePaths,//上传图片路径
            name: 'file',
            formData: {
              'user': 'test'
            },
            success:res=> {
              var data = res.data
              console.log(data,78945)
              _that.setData({
                tempFilePaths:data
              })
              // console.log(this,2222222222222222222222)
              wx.showModal({
                title: '提示',
                content: '上传成功',
              })
            }
          })
        }
      })
    },

  //弹出框蒙层截断touchmove事件
  preventTouchMove: function ()
   {
  },
  //隐藏模态对话框
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /* 对话框取消按钮点击事件*/
  onCancel: function () {
    this.hideModal();
  },
  /*对话框确认按钮点击事件*/
  onConfirm: function () {
    this.hideModal();
    console.log(this.data.endOil,555555)
    wx.getStorage({
      key: 'openid',
      success: (res) => {
        const datatravel = query(api.addtravel, 'post', { 
          openId: res.data, 
          endOil: this.data.endOil,
          endKm: this.data.endKm,
          id: this.data.dataMsg[0].orderNo, 
          travelNo: this.data.dataMsg[0].travelNo,
          travelStatus:3,
          picPath: this.data.tempFilePaths
          });

        datatravel.then(data =>{
          console.log(data,666666)
        }) 
      }
     })
    }
  }
})